import util from '../../util/util';

class UserModel {
  constructor() {
    // 保存接口数据
    this.svcRegWithWx = util.getCookie('$mall') || void 0;
    this.keep = false;
  }

  /** 初始化 */
  init(param) {
    return new Promise((resolve, reject) => {
      if (!util.getQueryStringByName('code')) {
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
          'wxff3d9c03665bae4f&redirect_uri=' +  window.$url+ param +'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
      } else {
        this.getUser().then(res => {
          resolve(res);
        }).catch(() => {
          reject('error');
        })
      }
    })
  }

  /**
   * 获取用户信息
   */
  getUser() {
    return new Promise((resolve, reject) => {
      window.$http.post(`${window.$apiUrl}/svcBusiUser/svcRegWithWx`, {
        code: window.location.search.split('=')[1],
        jsr_tjcode: "aaaaaaaa",
        jsr_uid: "bbbbbbbbb"
      }).then((res) => {
        if (res.data.info.hasOwnProperty('apptoken')) {
          this.svcRegWithWx = res.data.info;
          util.setCookie('$mall', res.data.info);
          this.keep = true;
          resolve(res.data.info);
        } else {
          reject('error');
        }
      })
    })
  }
}



export default new UserModel();